const xlsx = require('xlsx');
const fs = require('fs');

// 读取 Excel 文件
// const workbook = xlsx.readFile('translations2.xlsx');
const workbook = xlsx.readFile('translationsH5Ctrol.xlsx');


// 获取 Excel 文件中的第一个工作表
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];

// console.log(worksheet);

// 将工作表数据转换为 JSON 格式
const jsonData = xlsx.utils.sheet_to_json(worksheet, {defval: ''});


let de_DE_object = {}
let en_US_object = {}
let fr_FR_object = {}

jsonData.forEach((itemObject) => {

  //{
  //   'Key (DO NOT MODIFY)': 'howFindDeviceQ',
  //   'en_US (DO NOT MODIFY)': 'How to Find My Device?',
  //   de_DE: 'Wie kann ich mein Gerät koppeln?',
  //   'fr-FR': 'Comment trouver mon appareil ?'
  // }

  let key = itemObject['Key (DO NOT MODIFY)'];

  // console.log(key);

  // console.log(itemObject);
  if (!itemObject['de_DE']) {
    itemObject['de_DE'] = itemObject['en_US (DO NOT MODIFY)']
  }
  if (!itemObject['fr-FR']) {
    itemObject['fr-FR'] = itemObject['en_US (DO NOT MODIFY)']
  }

  for (const itemObjectKey in itemObject) {
    if (itemObjectKey.includes('en_US')) {
      en_US_object[key] = itemObject[itemObjectKey]
    }
    if (itemObjectKey === 'de_DE') {

      de_DE_object[key] = itemObject[itemObjectKey]
      // console.log(key+':'+itemObject[itemObjectKey]);
    }
    if (itemObjectKey === 'fr-FR') {
      fr_FR_object[key] = itemObject[itemObjectKey]
    }
  }

})

// 将 JSON 数据写入到文件
fs.writeFileSync('en_US.json', JSON.stringify(en_US_object,null,2));
fs.writeFileSync('de_DE.json', JSON.stringify(de_DE_object, null, 2));
fs.writeFileSync('fr_FR.json', JSON.stringify(fr_FR_object, null, 2));

console.log('Excel to JSON conversion completed!');
